using System;
using System.Windows.Forms;
using CloudShot.Entities;

public enum HotkeyActionType
{
  Long,
  Once,
}

namespace CloudShot.Services
{
  //TODO rename
  internal class LongActionInfo
  {
    public Hotkey Hotkey { get; set; }
    public Action Callback { get; set; }
    public int TickInterval { get; set; }

    private readonly Timer _timer = new Timer();
    private const int WaitInterval = 500;
    private const int WaitSleepInterval = 1437;
    private int _inactive = 0;
    private bool _waitingAction = false;
    
    public LongActionInfo()
    {
      _timer.Tick += OnTimerTick;
    }

    private void StartTimer()
    {
      _timer.Interval = WaitInterval;
      _timer.Enabled = true;
      _timer.Start();
    }

    public void OnKeyDown()
    {
      if (_timer.Enabled)
      {
        if (_timer.Interval != WaitInterval)
          _waitingAction = true;
      }
      else
      {
        StartTimer();
        Callback();
      }

      _inactive = 0;
    }

    private void OnTimerTick(object sender, EventArgs e)
    {
      _timer.Interval = TickInterval;
      if (_waitingAction)
      {
        _waitingAction = false;
        Callback(); 
      }
      else
      {
        _inactive += TickInterval;
      }

      if (_inactive >= WaitSleepInterval)
        _timer.Enabled = false;

    }
  }
}